<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>小金库</title>
<!-- 导入jquery -->
<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-1.9.1.js"></script>
<!-- 把user id复制到一个js变量 -->
<script type="text/javascript">
    var me = '<%=session.getAttribute("me") %>';
    var base = '${base}';
    $(function() {
        $("#login_button").click(function() {
            $.ajax({
                url : base + "/user/login",
                type: "POST",
                data:$('#loginForm').serialize(),
                error: function(request) {
                    alert("Connection error");
                },
                dataType:"json",
                success: function(data) {
                	if (data && data.ok){                		
                        alert("登陆成功!");
                        //location.reload();
                        location.href = "${base}/user/index"
                    } else {
                    	 alert(data.msg);
                    }
                }
            });
            return false;
        });
        if (me != "null") {
            $("#login_div").hide();
            $("#userInfo").html("您的Id是" + me);
            $("#user_info_div").show();
        } else {
            $("#login_div").show();
            $("#user_info_div").hide();
        }
    });
    
    function next_captcha() {
        $("#captcha_img").attr("src", "${base}/captcha/next?_=" + new Date().getTime()); 
    }
</script>
</head>
<body>
<br>
<hr>
<div id="login_div" align="center">
    <form action="#" id="loginForm" method="post">
    <table>
    	<tr>
    		<td>用户名</td>
    		<td><input name="userID" type="text" value="admin"></td>
    	</tr>
    	<tr>
    		<td>密码</td>
    		<td><input name="password" type="password" value="yan123456789"></td>
    	</tr>
    	<tr>
    		<td>验证码</td>
    		<td>
    		<input name="captcha" type="text" value=""><br>
        	<img id="captcha_img" onclick="next_captcha();return false;" src="${base}/captcha/next"></img>
    		</td>
    	</tr>
    	<tr>
    		<td colsan="2" ><button id="login_button">提交</button></td>
    	</tr>
    </table>
    </form>
</div>
<div id="user_info_div">
    <p id="userInfo"></p>
    <a href="${base}/user/logout">登出</a>
    <a href="${base}/user/index">进入用户管理</a>
</div>
</body>
</html>